<template>
  <common-list>
    <common-table
      v-loading="loading"
      slot="content"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="策略类型"
        align="left"
        prop="ruleName"
        width="250"
        show-overflow-tooltip
      />
      <el-table-column
        label="告警阈值"
        align="left"
        prop="threshold"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ scope.row.threshold }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="80px">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleAdd(scope.row)"
            v-hasPermi="['monitor:rule:edit']"
            >编辑
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <middleware-edit
      ref="middlewareEdit"
      @submitForm="submitForm"
    ></middleware-edit>
  </common-list>
</template>

<script>
import { fetchMonitorList } from "@/api/system/monitor";
import MiddlewareEdit from "./components/middlewareEdit.vue";
import BaseList from "@/mixins/BaseList";
export default {
  mixins: [BaseList],
  components: {
    MiddlewareEdit,
  },
  data() {
    return {
      ids: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ruleType: 1,
      },
      total: 0,
      list: [],
      loading: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      fetchMonitorList(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            res.rows.forEach((item) => {
              item.threshold =
                "内存 >" +
                (item.memLimit || 0) +
                "%；告警频率：" +
                (item.alarmRate || 0) +
                "分钟/次";
            });
            this.list = res.rows;
            this.total = res.total;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .catch(() => {
          this.list = [];
          this.total = 0;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    submitForm() {
      this.getList();
    },

    handleAdd(row) {
      this.$refs.middlewareEdit.show(row ? { ...row } : {});
    },
  },
};
</script>

<style lang="scss"></style>
